package b2.player.dataModel
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.player.event.MailEvent;
	import b2.widget.*;
	import b2.widget.warehouse.*;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	public class Mail extends EventDispatcher
	{
		public static const MAX_GIFT_NUM:int = 3;
		
		public var fromId:String;
		public var fromName:String;
		public var toId:String;
		
		public var message:String;
		
		public var giftArray:Array;
		
		public function Mail(fromId:String = "", toId:String = "", message:String = "", giftArray:Array = null)
		{
			this.fromId = fromId;
			this.toId = toId;
			this.message = message;
			if (giftArray != null)
				this.giftArray = giftArray;
			else
				this.giftArray = new Array();
			dispatchEvent(new MailEvent(MailEvent.MAIL_UPDATE));
		}

		public function setFromId(fromId:String):void
		{
			this.fromId = fromId;
			dispatchEvent(new MailEvent(MailEvent.MAIL_UPDATE));
		}
		
		public function setToId(toId:String):void
		{
			this.toId = toId;
			dispatchEvent(new MailEvent(MailEvent.MAIL_UPDATE));
		}
		
		public function setMessage(message:String):void
		{
			this.message = message;
			dispatchEvent(new MailEvent(MailEvent.MAIL_UPDATE));
		}
		
		public function setGiftArray(giftArray:Array):void
		{
			if (giftArray != null)
				this.giftArray = giftArray
			else
				this.giftArray = new Array();
			dispatchEvent(new MailEvent(MailEvent.MAIL_UPDATE));
		}
		
		public function addGift(gift:ObjWithNum):void
		{
			addObjectPackageItems();
			if (giftArray.length >= MAX_GIFT_NUM)
			{
				giftArray.splice(MAX_GIFT_NUM - 1, 1);
			}
			giftArray.push(gift);
			removeObjectPackageItems();
			Warehouse.getInstance().refresh();
			dispatchEvent(new MailEvent(MailEvent.MAIL_UPDATE));
		}
		
		public function removeGift(index:int):void
		{
			addObjectPackageItems();
			if (index < giftArray.length)
				giftArray.splice(index, 1);
			removeObjectPackageItems();
			Warehouse.getInstance().refresh();
			dispatchEvent(new MailEvent(MailEvent.MAIL_UPDATE));
		}
		
		public function addObjectPackageItems():void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			for (var i:int = 0; i < this.giftArray.length; i++)
			{
				var gift:ObjWithNum = giftArray[i];
				var obj:Obj = gift.obj;
				
				if (obj is Tank)
				{
					me.tankPackage.tankList.push((obj as Tank).tankId);
				}
				else
				{
					var comIndex:String = obj.objectIndex + "_" + obj.basicLevel + "_" + obj.strengthenLevel + "_" + 0;
					me.objPackage.objectList[comIndex] += gift.num;
				}
			}
		}
		
		public function removeObjectPackageItems():void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			for (var i:int = 0; i < this.giftArray.length; i++)
			{
				var gift:ObjWithNum = giftArray[i];
				var obj:Obj = gift.obj;
				
				if (obj is Tank)
				{
					me.tankPackage.deleteTank((obj as Tank).tankId);
				}
				else
				{
					var comIndex:String = obj.objectIndex + "_" + obj.basicLevel + "_" + obj.strengthenLevel + "_" + 0;
					me.objPackage.objectList[comIndex] -= gift.num;
				}
			}
		}
		
		public function createBalanceStr():String
		{
			var oilCost:int = -Config.basicConfig.postage;
			var balanceStr:String = PlayerInfoManager.getInstance().myUserId + "," + CommonObjConfig.OIL + ",0,0,0," + oilCost;
			for (var i:int = 0; i < giftArray.length; i++)
			{
				var gift:ObjWithNum = giftArray[i];
				var obj:Obj = gift.obj;
				if (!(obj is Tank))
				{
					balanceStr += "," + PlayerInfoManager.getInstance().myUserId + "," + obj.objectIndex + "," + obj.basicLevel + "," + 
					obj.strengthenLevel + ",0,-" + gift.num;
				}
			}
			return balanceStr;
		}
		
		public function send():void
		{
			var objBalance:String = createBalanceStr();
			ServerHelper.getInstance().postMail(this, objBalance);
			ServerHelper.getInstance().addEventListener(ServerEvent.POST_EMAIL, onPostMailFinished);
			dispatchEvent(new MailEvent(MailEvent.MAIL_UPDATE));
		}
		
		public function onPostMailFinished(event:Event):void
		{
			ServerHelper.getInstance().removeEventListener(ServerEvent.POST_EMAIL, onPostMailFinished);
			ServerHelper.getInstance().getPackageAndTank();
			ServerHelper.getInstance().addEventListener(ServerEvent.GET_PACKAGE_AND_TANK, onGetFinished);
		}
		
		public function onGetFinished(event:Event):void
		{
			ServerHelper.getInstance().removeEventListener(ServerEvent.GET_PACKAGE_AND_TANK, onGetFinished);
			Warehouse.getInstance().refresh();
        	var popMessage:PopMessage = new PopMessage("邮件发送成功！", 0x00FF00);
        	WidgetManager.getInstance().popOut([popMessage], false, true);
		}
	}
}